<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="3" :lg="6" :md="6" :sm="24">
            <a-form-item label="出库单号">
              <a-input placeholder="" v-model="queryParam.id"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="6" :md="6" :sm="24">
            <a-form-item label="来源编号">
              <a-input placeholder="" v-model="queryParam.sourceId"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="6" :md="6" :sm="24">
            <a-form-item label="状态">
              <!--状态这里换一下数据字典或自己配置-->
              <j-dict-select-tag v-model="queryParam.status" placeholder="请选择状态" dictCode="inout_status"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="6" :sm="24">
            <a-form-item label="创建时间">
              <a-range-picker format="YYYY-MM-DD"
                              :placeholder="['开始时间', '结束时间']"
                              @change="onTimeChange"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>


    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>
        <span>已选择</span>
        <a style="font-weight: 600">
          {{ selectedRowKeys.length }}
        </a>
        <span>项</span>
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type:type}"
        @change="handleTableChange"
        :customRow="clickThenCheck">

        <span slot="action" slot-scope="text, record">
          <a :hidden="[5,7].indexOf(record.status) === -1" @click="handleInRecord(record)">出库记录</a>
           <a :hidden="[5,7].indexOf(record.status) === -1" style="padding-left:10px;" @click="handleExportXls('出库单报表',record)">导出出库记录</a>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="出库明细" :key="refKeys[0]" :forceRender="true">
        <stmStockOutItem-ListJX ref="StmStockOutItemListJX"></stmStockOutItem-ListJX>
      </a-tab-pane>
    </a-tabs>

    <!-- 表单区域 -->
    <!--    <stmStockInout-modal ref="modalForm" @ok="modalFormOk"/>-->
    <stm-stock-in-add-modalJX ref="addModalForm" @ok="modalFormOk"/>
    <stm-stock-out-record-modalJX ref="StmStockOutRecordModalJX"/>

  </a-card>
</template>

<script>
import {httpAction, putAction, postAction, downFile} from '@/api/manage'
import StmStockInAddModalJX from './modules/StmStockInAddModalJX'
import StmStockOutRecordModalJX from './modules/StmStockOutRecordModalJX'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import StmStockInoutModal from './modules/StmStockInoutModal'
import { deleteAction } from '@/api/manage'
import StmStockOutItemListJX from './StmStockOutItemListJX'
import StmStockInoutItemModal from './modules/StmStockInoutItemModal'
import JDate from '@/components/jeecg/JDate'


export default {
  name: 'StmSendOrderStockOutList',
  mixins: [JeecgListMixin],
  components: {
    StmStockInoutModal,
    StmStockInoutItemModal,
    StmStockOutItemListJX,
    StmStockInAddModalJX,
    StmStockOutRecordModalJX,
    JDate
  },
  data() {
    return {
      refKeys: ['stmStockInoutItem'],
      description: '出库单管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '出库单号',
          align: 'center',
          dataIndex: 'id'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          //1：待出库/待入库，4:已取消，5：部分发货/收货，7：已完成/已出库/已入库
          customRender: function (text) {
            if (text === 1) {
              return '待出库'
            } else if (text === 4) {
              return '已取消'
            } else if (text === 5) {
              return '部分发货'
            }else if (text === 7) {
              return '已出库'
            }
          }
        },
        {
          title: '来源编号',
          align: 'center',
          dataIndex: 'sourceIdStr'
        },
        {
          title: '发货单',
          align: 'center',
          dataIndex: 'sendOrderId'
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy'
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '操作人',
          align: 'center',
          dataIndex: 'updateBy'
        },
        {
          title: '操作时间',
          align: 'center',
          dataIndex: 'updateTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],

      //操作url
      type: 'radio',
      url: {
        list: '/stm/stmStockInout/list',
        delete: '/stm/stmStockInout/cancel',
        deleteBatch: '/stm/stmStockInout/deleteBatch',
        confirmBatch: '/stm/stmStockInout/confirmBatch',
        exportXlsUrl: '/stm/stmStockInout/exportXls'
      }
    }
  },
  created() {
    this.queryParam.type = 'STOCK_OUT';
    this.queryParam.bizType =2;
  },
  methods: {
    clickThenCheck(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.toString().split(','), [record])
          }
        }
      }
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.$refs.StmStockOutItemListJX.getMain(this.selectedRowKeys[0])
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.$refs.StmStockOutItemListJX.queryParam.mainId = null
      this.$refs.StmStockOutItemListJX.loadData()
      this.$refs.StmStockOutItemListJX.selectedRowKeys = []
      this.$refs.StmStockOutItemListJX.selectionRows = []
    },
    onTimeChange: function(value, dateString) {
      console.log(dateString[0], dateString[1])
      this.queryParam.startTime = dateString[0]
      this.queryParam.endTime = dateString[1]
    },
    searchQuery: function() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.$refs.StmStockOutItemListJX.queryParam.mainId = null
      this.$refs.StmStockOutItemListJX.loadData()
      this.$refs.StmStockOutItemListJX.selectedRowKeys = []
      this.$refs.StmStockOutItemListJX.selectionRows = []
      this.loadData()
    },
    handleConfirm: function() {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        let ids = []
        let index = this.selectionRows.findIndex(item => {
          if ( item.status == 4 || item.status == 7) {
            return true;
          }
        })
        if(index !== -1) {
          this.$message.warning('已取消或已完成的单据不可进行出出库操作！')
          return
        }
        for (let a = 0; a < this.selectedRowKeys.length; a++) {
          ids.push(this.selectedRowKeys[a])
        }
        let that = this
        this.$confirm({
          title: '确认出出库操作',
          content: '是否确认选中的出出库单?',
          onOk: function() {
            that.loading = true
            postAction(that.url.confirmBatch, ids ).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.loading = false
            })
          }
        })
      }
    },
    handleInRecord:function(record){
      this.$refs.StmStockOutRecordModalJX.show(record);
    },
    handleExportXls(fileName,record){
      if(!fileName || typeof fileName != "string"){
        fileName = "导出文件"
      }
      let param = {id:record.id};
      downFile(this.url.exportXlsUrl,param).then((data)=>{
        if (!data) {
          this.$message.warning("文件下载失败")
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xls')
        }else{
          let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName+'.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link); //下载完成移除元素
          window.URL.revokeObjectURL(url); //释放掉blob对象
        }
      })
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less'
</style>
